package cn.iocoder.yudao.module.member.controller.admin.confpoint;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.member.controller.admin.confpoint.vo.MemberConfPointPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.confpoint.vo.MemberConfPointRespVO;
import cn.iocoder.yudao.module.member.controller.admin.confpoint.vo.MemberConfPointSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.memberconf.confpoint.MemberConfPointDO;
import cn.iocoder.yudao.module.member.service.memberconf.confpoint.MemberConfPointService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 积分奖励规则")
@RestController
@RequestMapping("/member/conf-point")
@Validated
public class MemberConfPointController {

    @Resource
    private MemberConfPointService confPointService;

    @PostMapping("/create")
    @Operation(summary = "创建积分奖励规则")
    @PreAuthorize("@ss.hasPermission('member:conf-point:create')")
    public CommonResult<Long> createConfPoint(@Valid @RequestBody MemberConfPointSaveReqVO createReqVO) {
        return success(confPointService.createConfPoint(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新积分奖励规则")
    @PreAuthorize("@ss.hasPermission('member:conf-point:update')")
    public CommonResult<Boolean> updateConfPoint(@Valid @RequestBody MemberConfPointSaveReqVO updateReqVO) {
        confPointService.updateConfPoint(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除积分奖励规则")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:conf-point:delete')")
    public CommonResult<Boolean> deleteConfPoint(@RequestParam("id") Long id) {
        confPointService.deleteConfPoint(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得积分奖励规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:conf-point:query')")
    public CommonResult<MemberConfPointRespVO> getConfPoint(@RequestParam("id") Long id) {
        MemberConfPointDO confPoint = confPointService.getConfPoint(id);
        return success(BeanUtils.toBean(confPoint, MemberConfPointRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得积分奖励规则分页")
    @PreAuthorize("@ss.hasPermission('member:conf-point:query')")
    public CommonResult<PageResult<MemberConfPointRespVO>> getConfPointPage(@Valid MemberConfPointPageReqVO pageReqVO) {
        PageResult<MemberConfPointDO> pageResult = confPointService.getConfPointPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MemberConfPointRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出积分奖励规则 Excel")
    @PreAuthorize("@ss.hasPermission('member:conf-point:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConfPointExcel(@Valid MemberConfPointPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MemberConfPointDO> list = confPointService.getConfPointPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "积分奖励规则.xls", "数据", MemberConfPointRespVO.class,
                        BeanUtils.toBean(list, MemberConfPointRespVO.class));
    }

}